<template>
  <div class="table">
    <div class="container">
      <div class="handle-box">
        <el-input
          v-model="select_word"
          class="handle-input mr10"
          size="small"
          placeholder="请输入歌手名字"
        ></el-input>
        <el-button
          type="primary"
          size="small"
          @click="centerDialogVisible = true"
          >添加歌手</el-button
        >
        <el-button type="primary" size="small" @click="delAll"
          >批量删除</el-button
        >
      </div>
    </div>
    <el-table
      ref="multipleTable"
      border
      size="small"
      style="width: 100%"
      height="580px"
      :data="data"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="40"></el-table-column>
      <el-table-column label="歌手图片" width="110" align="center">
        <template v-slot="scope">
          <div class="singer-img">
            <el-image
              :fit="fits"
              :src="getUrl(scope.row.pic)"
              alt=""
              style="width: 100%"
            ></el-image>
          </div>
          <el-upload
            class="upload-demo"
            :action="uploadUrl(scope.row.id)"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <el-button size="small">更新图片</el-button>
          </el-upload>
        </template>
      </el-table-column>
      <el-table-column
        prop="name"
        label="歌手"
        width="120"
        align="center"
      ></el-table-column>
      <el-table-column label="性别" width="50" align="center">
        <template v-slot="scope">
          <div>{{ changeSex(scope.row.sex) }}</div>
        </template>
      </el-table-column>
      <el-table-column label="出生" width="120" align="center">
        <template v-slot="scope">
          <div>{{ getBirth(scope.row.birth) }}</div>
        </template>
      </el-table-column>
      <el-table-column
        prop="location"
        label="地区"
        width="100"
        align="center"
      ></el-table-column>
      <el-table-column label="简介">
        <template v-slot="scope">
          <p style="height: 100px; overflow: scroll">
            {{ scope.row.introduction }}
          </p>
        </template>
      </el-table-column>
      <el-table-column label="歌曲管理" width="110" align="center">
        <template v-slot="scope">
          <el-button
            size="small"
            @click="songEdit(scope.row.id, scope.row.name)"
            >歌曲管理</el-button
          >
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150" align="center">
        <template v-slot="scope">
          <el-button size="small" @click="handleEdit(scope.row)"
            >编辑</el-button
          >
          <el-button
            size="small"
            type="danger"
            @click="handleDelete(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination
        background
        layout="total, prev, pager, next"
        :current-page="currentPage"
        :page-size="pageSize"
        :total="tableData.length"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </div>
    <!-- 添加歌手弹出框 -->
    <el-dialog
      title="添加歌手"
      :visible.sync="centerDialogVisible"
      width="400px"
      center
    >
      <el-form
        class="demo-ruleForm"
        :model="registerForm"
        status-icon
        ref="registerForm"
        label-width="80px"
      >
        <el-form-item prop="name" label="歌手名" size="small">
          <el-input v-model="registerForm.name" placeholder="歌手名"></el-input>
        </el-form-item>
        <el-form-item label="性别" size="small">
          <el-radio-group v-model="registerForm.sex">
            <el-radio :label="0">女</el-radio>
            <el-radio :label="1">男</el-radio>
            <el-radio :label="2">组合</el-radio>
            <el-radio :label="3">不明</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item prop="location" label="故乡" size="small">
          <el-input
            v-model="registerForm.location"
            placeholder="故乡"
          ></el-input>
        </el-form-item>
        <el-form-item prop="birth" label="出生" size="small">
          <el-date-picker
            type="date"
            placeholder="选择日期"
            v-model="registerForm.birth"
            style="width: 100%"
            format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd"
          ></el-date-picker>
        </el-form-item>
        <el-form-item prop="introduction" label="歌手介绍" size="small">
          <el-input
            v-model="registerForm.introduction"
            type="textarea"
            placeholder="歌手介绍"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button size="small" @click="centerDialogVisible = false"
            >取 消</el-button
          >
          <el-button type="primary" size="small" @click="addsinger"
            >确 定</el-button
          >
        </span>
      </template>
    </el-dialog>

    <!-- 编辑弹出框 -->
    <el-dialog title="编辑" :visible.sync="editVisible" width="400px">
      <el-form ref="editorForm" :model="editorForm" label-width="60px">
        <el-form-item label="歌手" size="small">
          <el-input v-model="editorForm.name"></el-input>
        </el-form-item>
        <el-form-item label="性别" size="small" prop="sex">
          <el-radio-group v-model="editorForm.sex">
            <el-radio :label="'0'">女</el-radio>
            <el-radio :label="'1'">男</el-radio>
            <el-radio :label="'2'">组合</el-radio>
            <el-radio :label="'3'">不明</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="生日" size="small">
          <el-date-picker
            type="date"
            placeholder="选择日期"
            v-model="editorForm.birth"
            format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd"
            style="width: 100%"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="地区" size="small">
          <el-input v-model="editorForm.location"></el-input>
        </el-form-item>
        <el-form-item label="简介" size="small">
          <el-input
            type="textarea"
            v-model="editorForm.introduction"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button size="small" @click="editVisible = false">取 消</el-button>
          <el-button type="primary" size="small" @click="saveEdit"
            >确 定</el-button
          >
        </span>
      </template>
    </el-dialog>

    <!-- 删除歌手弹出框 -->
    <el-dialog title="删除歌手" :visible.sync="delVisible" width="400px">
      <div class="center">删除不可恢复，是否确定删除？</div>
      <template #footer>
        <span class="dialog-footer">
          <el-button size="small" @click="delVisible = false">取 消</el-button>
          <el-button type="primary" size="small" @click="deleteRow"
            >确 定</el-button
          >
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { setSinger } from "../api/index";
import { getAllSinger } from "../api/index";
import { updateSinger } from "../api/index";
import { delSinger } from "../api/index";
import { mixin } from "../mixins";
export default {
  mixins: [mixin],
  data() {
    return {
      fits: "cover",
      //添加歌手对话框
      centerDialogVisible: false,
      //编辑对话框
      editVisible: false,
      //删除对话框
      delVisible: false,
      tempDate: [],
      select_word: "",

      idx: -1,

      //编辑框
      editorForm: {
        // 编辑框信息
        id: "",
        name: "",
        sex: "",
        pic: "",
        birth: "",
        location: "",
        introduction: "",
      },
      registerForm: {
        // 添加框信息
        name: "",
        sex: "",
        birth: "",
        pic: "",
        location: "",
        introduction: "",
      },
      tableData: [],

      //批量删除数组
      multipleSelection: [],

      pageSize: 4, // 页数
      currentPage: 1, // 当前页
    };
  },
  computed: {
    //计算当前搜索结果的数据
    data() {
      return this.tableData.slice(
        (this.currentPage - 1) * this.pageSize,
        this.currentPage * this.pageSize
      );
    },
  },
  watch: {
    //搜索框内容监听
    select_word: function () {
      if (this.select_word === "") {
        this.tableData = this.tempDate;
      } else {
        this.tableData = [];
        for (let item of this.tempDate) {
          if (item.name.includes(this.select_word)) {
            this.tableData.push(item);
          }
        }
      }
    },
  },
  created() {
    this.getData();
  },
  methods: {
    //获取当前页
    handleCurrentChange(val) {
      this.currentPage = val;
    },
    uploadUrl(id) {
      return `${this.$store.state.HOST}/singer/updateSingerPic?id=${id}`;
    },
    // 获取歌手
    getData() {
      const _this = this;
      _this.tableData = [];
      _this.tempDate = [];
      getAllSinger().then((res) => {
        _this.tableData = res;
        _this.tempDate = res;
        this.currentPage = 1
      });
    },
    // 添加歌手
    addsinger() {
      const _this = this;
      _this.registerForm.pic = "/img/singerPic/Firstavator.jpeg";
      setSinger(_this.registerForm)
        .then((res) => {
          if (res.code === 1) {
            _this.centerDialogVisible = false;
            _this.getData();
            _this.registerForm = {};
            _this.$notify({
              title: res.msg,
              type: "success",
            });
          } else {
            _this.$notify({
              title: res.msg,
              type: "error",
            });
          }
        })
        .catch((err) => {
          console.error(err);
        });
    },

    //编辑
    handleEdit(row) {
      this.editVisible = true;
      this.editorForm = {
        id: row.id,
        name: row.name,
        sex: row.sex,
        pic: row.pic,
        birth: row.birth,
        location: row.location,
        introduction: row.introduction,
      };
    },
    // 保存编辑
    saveEdit() {
      const _this = this;
      updateSinger(_this.editorForm)
        .then((res) => {
          if (res.code === 1) {
            _this.getData();
            _this.$notify({
              title: "编辑成功",
              type: "success",
            });
          } else {
            _this.$notify({
              title: "编辑失败",
              type: "error",
            });
          }
        })
        .catch((err) => {
          console.error(err);
        });
      _this.editVisible = false;
    },
    //删除一名歌手
    deleteRow() {
      const _this = this;
      let id = _this.idx;
      delSinger(id).then((res) => {
        if (res.code === 1) {
          _this.getData();
          _this.$notify({
            title: "删除成功",
            type: "success",
          });
          _this.delVisible = false;
        } else {
          _this.$notify({
            title: "删除失败",
            type: "error",
          });
        }
      });
    },

    //歌曲管理
    songEdit(id, name) {
      this.$router.push({ path: `/Song`, query: { id: id, name: name } });
    },
  },
};
</script>

<style scoped>
.handle-box {
  margin-bottom: 20px;
}
.handle-input {
  width: 300px;
  display: inline-block;
}
.singer-img {
  width: 100%;
  height: 80px;
  border-radius: 5px;
  margin-bottom: 5px;
  overflow: hidden;
}
.pagination {
  display: flex;
  justify-content: center;
}
</style>